Take-home exercise01

Author

YUAN yihao

Getting started

install and launching R packages

pacman::p_load(ggiraph, plotly, 
               patchwork, DT, tidyverse) 

Importing the data

res_data <- read_csv("respopagesex2024/respopagesex2024.csv")
Rows: 60424 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): PA, SZ, Age, Sex
dbl (2): Pop, Time

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
res_data$Age <- as.numeric(as.character(res_data$Age))
Warning: NAs introduced by coercion
str(res_data)
spc_tbl_ [60,424 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ PA  : chr [1:60424] "Ang Mo Kio" "Ang Mo Kio" "Ang Mo Kio" "Ang Mo Kio" ...
 $ SZ  : chr [1:60424] "Ang Mo Kio Town Centre" "Ang Mo Kio Town Centre" "Ang Mo Kio Town Centre" "Ang Mo Kio Town Centre" ...
 $ Age : num [1:60424] 0 0 1 1 2 2 3 3 4 4 ...
 $ Sex : chr [1:60424] "Males" "Females" "Males" "Females" ...
 $ Pop : num [1:60424] 10 10 10 10 10 10 10 10 30 10 ...
 $ Time: num [1:60424] 2024 2024 2024 2024 2024 ...
 - attr(*, "spec")=
  .. cols(
  ..   PA = col_character(),
  ..   SZ = col_character(),
  ..   Age = col_character(),
  ..   Sex = col_character(),
  ..   Pop = col_double(),
  ..   Time = col_double()
  .. )
 - attr(*, "problems")=<externalptr> 
library(ggplot2)
library(plotly)
library(dplyr)

# 假设 res_data 是原始长格式:Age, Sex (Males/Females), Pop

# 设置颜色
sex_colors <- c("Males" = "steelblue", "Females" = "tomato")

# 创建 tooltip 字段
plot_data <- res_data %>%
  mutate(tooltip = paste("Age:", Age, "<br>Sex:", Sex, "<br>Pop:", Pop))

# 画图
p <- ggplot(plot_data, aes(x = Age, y = Pop, fill = Sex, text = tooltip)) +
  geom_col(position = "stack") +
  scale_fill_manual(values = sex_colors) +
  labs(
    x = "Age", y = "Population",
    title = "Stacked Histogram of Population by Age and Sex"
  ) +
  theme_minimal()

# 转为交互式
ggplotly(p, tooltip = "text")